import Vue from 'vue'
import ImEnum from './enums/index.js'
import ImStorage from './utils/ImStorage.js'
import SocketApi from './api/SocketApi.js'
/**
 * api
 */
import BenderChatApi from './api/chat-api.js'

Vue.prototype.BenderSdk = {
  /**
   * @param {Object}
   *  websocke是否连接成功
   */
  loginState: SocketApi.socketOpen,
  /**
   * 登录webSocket
   * @param userName
   * @param passWord
   */
  login: function (userName, passWord) {
    /**
     * 连接im服务
     */
    if (!SocketApi.socketOpen && userName && passWord) {
      if (SocketApi.connectSocket(userName, passWord)) {
        console.log('登录成功---')
        /**
         * 本地缓存账号密码
         * @type {{passWord: string, userName: string}}
         */
        let nameAndPwd = {
          userName: userName,
          passWord: passWord,
          createTime:new Date().getTime()
        }
        ImStorage.setImStorage(ImEnum.ImStorageEnum.loginNameAndPaw, nameAndPwd)
      }else{
        console.log('重复登录---')
      }
    }
  },
  /**
   * 会话页相关api
   */
  ChatApi: BenderChatApi,
  /**
   * 获取当前登录人的信息
   */
  sendMessage: function (msg) {
    // const j_im_send_req = {
    // 	chatType: 1,
    // 	cmd: 11,
    // 	content: JSON.stringify(msg),
    // 	createTime: new Date().getMilliseconds(),
    // 	from: msg.sender,
    // 	group_id: '100',
    // 	msgType: 0
    // }
    SocketApi.sendSocketMessage(msg)
  },
  getLoginUserInfo: function () {
    return ImStorage.getImStorage(ImEnum.ImStorageEnum.loginUserInfo)
  }


}
